﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 洞口信息，由附着在面上的 <see cref="ComponentInstance"/> 和 <see cref="Group"/> 造成的洞口
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUOpeningRef 的包装</remarks>
    public class Opening : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 获取洞口包含的点的个数
        /// </summary>
        /// <param name="opening"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOpeningGetNumPoints(IntPtr opening, out long count);

        /// <summary>
        /// 获取洞口包含的全部点
        /// </summary>
        /// <param name="opening"></param>
        /// <param name="len"></param>
        /// <param name="points"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOpeningGetPoints(IntPtr opening, long len, Point3D[] points, out long count);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="opening"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUOpeningRelease(ref IntPtr opening);
        #endregion

        /// <summary>
        /// 洞口包括的点
        /// </summary>
        public List<Point3D> Points
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUOpeningGetNumPoints(Id, out long count));
                Point3D[] points = new Point3D[count];
                SketchUpExceptionHelper.ThrowSUResult(SUOpeningGetPoints(Id, count, points, out long realcount));
                return points.ToList();
            }
        }

        internal Opening(IntPtr id) => Id = id;

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUOpeningRelease(ref Id));
        }
    }
}